package org.rcloud.io.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.rcloud.io.Utils.PageVo;
import org.rcloud.io.Utils.ResultVo;
import org.rcloud.io.model.Scenery;
import org.rcloud.io.service.SceneryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/3/12.
 */
@RestController
@RequestMapping(value = "/scenery")
public class SceneryController {

    @Autowired
    private SceneryService sceneryService;

    /*
  * 获取所有的风景线路信息
  * */
    @ApiOperation(value="获取所有的风景线路信息", notes="获取所有的风景线路信息")
    @ResponseBody
    @RequestMapping(value = "/findScenery", produces = {"application/json;charset=UTF-8"})
    public PageVo<Scenery> findScenery(){
        return sceneryService.findScenery();
    }


    /*
    * 根据eid获取风景线路信息
    * */
    @ApiOperation(value="根据eid获取风景线路信息", notes="根据eid获取风景线路信息")
    @ResponseBody
    @RequestMapping(value = "/findByEid", produces = {"application/json;charset=UTF-8"})
    public ResultVo findByEid(@RequestBody String params){
        JSONObject object= JSON.parseObject(params);
        return sceneryService.findByEid(object.getString("eid"));
    }

    /*
   * 根据id获取风景线路信息
   * */
    @ApiOperation(value="根据id获取风景线路信息", notes="根据id获取风景线路信息")
    @ResponseBody
    @RequestMapping(value = "/findById", produces = {"application/json;charset=UTF-8"})
    public ResultVo findById(@RequestBody String params){
        String[] ids = params.split("=");
        int id=Integer.parseInt(ids[1]);
        return sceneryService.findById(id);
    }


     /*
    * 保存风景线路信息
    * */
     @ApiOperation(value="保存风景线路信息", notes="保存风景线路信息")
     @ResponseBody
     @RequestMapping(value = "/saveScenery", produces = {"application/json;charset=UTF-8"})
     public ResultVo saveScenery(@RequestBody String params){

         JSONObject object =JSON.parseObject(params);

         Map map = JSON.parseObject(object.toString());
         String eid = map.get("eid").toString();
         String name = map.get("name").toString();
         String address = map.get("address").toString();
         String longitude = map.get("longitude").toString();
         String latitude = map.get("latitude").toString();
         String etype = map.get("etype").toString();
         String summary = map.get("summary").toString();
         String report = map.get("report").toString();
         String username = map.get("username").toString();
         String userphone = map.get("userphone").toString();

         Scenery scenery = new Scenery();
         scenery.setEid(eid);
         scenery.setName(name);
         scenery.setAddress(address);
         scenery.setLongitude(longitude);
         scenery.setLatitude(latitude);
         scenery.setEtype(etype);
         scenery.setSummary(summary);
         scenery.setReport(report);
         scenery.setUsername(username);
         scenery.setUserphone(userphone);
         scenery.setFlag(1);

         return sceneryService.saveScenery(scenery);
     }

    /*
        * 修改风景线路信息
        * */
    @ApiOperation(value="修改风景线路信息", notes="修改风景线路信息")
    @ResponseBody
    @RequestMapping(value = "/updateScenery", produces = {"application/json;charset=UTF-8"})
    public ResultVo updateScenery(@RequestBody String params){
        JSONObject object =JSON.parseObject(params);
        Map map = JSON.parseObject(object.toString());

        int id = Integer.parseInt(map.get("id").toString());
        String eid = map.get("eid").toString();
        String name = map.get("name").toString();
        String address = map.get("address").toString();
        String longitude = map.get("longitude").toString();
        String latitude = map.get("latitude").toString();
        String etype = map.get("etype").toString();
        String summary = map.get("summary").toString();
        String report = map.get("report").toString();
        String username = map.get("username").toString();
        String userphone = map.get("userphone").toString();

        Scenery scenery = new Scenery();
        scenery.setId(id);
        scenery.setEid(eid);
        scenery.setName(name);
        scenery.setAddress(address);
        scenery.setLongitude(longitude);
        scenery.setLatitude(latitude);
        scenery.setEtype(etype);
        scenery.setSummary(summary);
        scenery.setReport(report);
        scenery.setUsername(username);
        scenery.setUserphone(userphone);

        return sceneryService.updateScenery(scenery);
    }
}
